package com.wyh.cloud.order.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.wyh.cloud.order.feign.OrderFeignClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import pojo.CommonResult;

import javax.annotation.Resource;

/**
 * @author wyh
 * @version 1.0
 * @date 2021/11/26 10:36
 */
@RestController
@Slf4j
public class OrderController {


    @Autowired
    private OrderFeignClient feignClient;

    @GetMapping(value = "/consumer/payment/findAll/{id}")
    //@SentinelResource(value = "fallback")   //没有配置
    //@SentinelResource(value = "fallback",fallback = "findAllFallBack")  //服务降级方法,只负责业务异常处理
    //@SentinelResource(value = "fallback",blockHandler = "blockHandler")  //只负责sentinel控制台配置降级控制
    @SentinelResource(value = "fallback",
            fallback = "findAllFallBack",
            blockHandler = "blockHandler",
            exceptionsToIgnore = {IllegalArgumentException.class})  //都打开
    public CommonResult findAll(@PathVariable("id") Integer id) {
        CommonResult result = feignClient.findAll();

        if (id == 4){
            throw new IllegalArgumentException("id值参数异常");
        }else if (id == 5){
            throw new NullPointerException("数据为空异常");
        }

        return result;
    }
    public CommonResult findAllFallBack( Integer id, Throwable e){
        return new CommonResult(1001,"服务降级   "+id+"   "+e);
    }

    public CommonResult blockHandler(Integer id, BlockException e){
        return new CommonResult(1002,"sentinel配置    "+id+"   "+e);
    }
}
